package com.qyl.deguru.controller.admin;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.qyl.deguru.common.PageConstant;
import com.qyl.deguru.common.PageModel;
import com.qyl.deguru.dto.Shop;
import com.qyl.deguru.execption.DeguruException;
import com.qyl.deguru.service.ShopService;
import com.qyl.deguru.service.impl.ShopServiceImple;
import com.qyl.deguru.utils.ExcelUtils;
import com.qyl.deguru.utils.UploadUtil;

/**
 * 商品控制类
 */
@Controller
@RequestMapping("/admin/shop")
public class ShopController {
	ShopService shopService = new ShopServiceImple();

	// 跳转到商品详情页面
	@RequestMapping("/toShopList")
	public String toShopList(ModelMap modelmap, Shop shop,String pageIndex,String pageSize) {
		PageModel pageModel = new PageModel();
		// 当前页为空，设置当前页=0
		if ("".equals(pageIndex) || pageIndex == null) {
			pageModel.setPageIndex(0);
		} else {
			pageModel.setPageIndex(Integer.parseInt(pageIndex));
		}
		// 判断每页显示的记录是否大于0
		if (pageSize != null && pageSize != "") {
			pageModel.setPageSize(Integer.parseInt(pageSize));
		}
		// 获取总记录数
		int recordcount = shopService.getcount();
		System.out.println("getcount" + recordcount);
		pageModel.setRecordCount(recordcount);
		// 显示商品详情信息
		List<Shop> shops = shopService.showShopList(shop,pageModel);
		// 将商品详情信息，回显信息，分页存储到缓存中
		modelmap.addAttribute("shops", shops);
		modelmap.addAttribute("shop", shop);
		modelmap.addAttribute("pageModel", pageModel);
		return "shop/shopList";
	}
	//根据id删除商品信息
	@RequestMapping("/delShop")
	public String delShop(int id,ModelMap modelmap) {
		int count=shopService.delShopById(id);
		if(count>0) {
			modelmap.addAttribute(PageConstant.TIP, "删除成功");
		}else {
			modelmap.addAttribute(PageConstant.TIP,"删除失败");
		}
		return "forward:toShopList";
	}
	//批量删除
	@RequestMapping("/delShops")
	public String delShops(ModelMap modelmap,int[] ids) {
		int count=shopService.delShops(ids);
		if(count>0) {
			modelmap.addAttribute(PageConstant.TIP, "删除成功");
		}else {
			modelmap.addAttribute(PageConstant.TIP, "删除失败");
		}
		return "forward:toShopList";
	}
	//跳转到新增页面
	@RequestMapping("/toAddShop")
	public String toAddShop() {
		return "shop/shopAdd";
	}

	// 异步上传图片
	@RequestMapping("/uploadpic")
	@ResponseBody
	public Map<String, String> uploadPic(@RequestParam("myfile") MultipartFile file, Shop shop, ModelMap modelmap) {
		// 图片
		Map<String, String> map = new HashMap<String, String>();
		try {
			String image = UploadUtil.loadFile(file);
			shop.setImage(image);
			map.put("image", image);
			modelmap.addAttribute("image", image);
			map.put("status", "success");
		} catch (Exception e) {
			System.out.println("图片上传失败");
		}
		return map;
	}

	//新增商品信息
	@RequestMapping("/addShop")
	public String addShop(ModelMap modelmap,Shop shop) {
		shop.setCreate_date(new Date());
		shop.setGrounding_date(new Date());
		System.out.println(shop+"----");
		int count=shopService.addShop(shop);
		if(count>0) {
			modelmap.addAttribute(PageConstant.TIP, "新增成功 ");
		}else {
			modelmap.addAttribute(PageConstant.TIP, "新增失败");
		}
		return "shop/shopAdd";
	}
	//跳转到更新界面
	@RequestMapping("/toShopUpdate")
	public String toShopUpdate(ModelMap modelmap,int id) {
		//根据id获取商品信息
		Shop shop=shopService.getShopById(id);
		modelmap.addAttribute("shop", shop);
		return "shop/shopUpdate";
	}
	//更新商品数据
	@RequestMapping("/shopUpdate")
	public String shopUpdate(ModelMap modelmap,Shop shop,String newImage) {
		shop.setImage(newImage);
		int count=shopService.shopUpdate(shop);
		if(count>0) {
			modelmap.put(PageConstant.TIP, "更新成功 ");
		}else {
			modelmap.put(PageConstant.TIP, "更新失败");
		}
		return "shop/shopUpdate";
	}
	@RequestMapping("/exportExcel")
	public void exportExcel(HttpServletResponse response,HttpServletRequest request){
		//根据业务层，获取数据库用户列表信息
		List<Shop> shops = shopService.getShop();
		String excelName = "商品execl";
		String sheetName="商品sheet";
		String[] headers = {"ID","标题","品牌名称","价格","优惠价格","上架日期","库存","图片","描述","类型","用户","创建日期","热销度"};
		try{
			//excel导出
			ExcelUtils.exportExcel(excelName, sheetName, headers, shops, response, request);
		}catch(Exception ex){
			throw new DeguruException("导出商品信息Excel失败!");
		}
	}
}
